//方法一
export function formatDate(val) {
	var date = new Date(Number(val)); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + "-";
	var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
	var D = date.getDate() + " ";
	var h = date.getHours() + ":";
	var m = date.getMinutes() + ":";
	var s = (date.getSeconds() < 10 ? "0" + (date.getSeconds()) : date.getSeconds());
	return Y + M + D + h + m + s;
}

//方法二
export function formatDates(date, fmt) {
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	};
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + '';
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
		}
	}
	return fmt;
};

function padLeftZero(str) {
	return ('00' + str).substr(str.length);
}


export function isDateBetween(dateString, startDateString, endDateString) {
	if (isEmpty(dateString)) {
		alert("dateString不能为空");
		return;
	}
	if (isEmpty(startDateString)) {
		alert("startDateString不能为空");
		return;
	}
	if (isEmpty(endDateString)) {
		alert("endDateString不能为空");
		return;
	}
	var flag = false;
	var startFlag = (dateCompare(dateString, startDateString) < 1);
	var endFlag = (dateCompare(dateString, endDateString) > -1);
	if (startFlag && endFlag) {
		flag = true;
	}
	return flag;
};

function dateCompare(dateString, compareDateString) {
	if (isEmpty(dateString)) {
		alert("dateString不能为空");
		return;
	}
	if (isEmpty(compareDateString)) {
		alert("compareDateString不能为空");
		return;
	}
	var dateTime = dateParse(dateString).getTime();
	var compareDateTime = dateParse(compareDateString).getTime();
	if (compareDateTime > dateTime) {
		return 1;
	} else if (compareDateTime == dateTime) {
		return 0;
	} else {
		return -1;
	}
};

function dateParse(dateString) {
	// var SEPARATOR_BAR = "-";
	// var SEPARATOR_SLASH = "/";
	// var SEPARATOR_DOT = ".";
	// var dateArray;
	// if (dateString.indexOf(SEPARATOR_BAR) > -1) {
	// 	dateArray = dateString.split(SEPARATOR_BAR);
	// } else if (dateString.indexOf(SEPARATOR_SLASH) > -1) {
	// 	dateArray = dateString.split(SEPARATOR_SLASH);
	// } else {
	// 	dateArray = dateString.split(SEPARATOR_DOT);
	// }
	// return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]);
	return new Date(dateString);
};

export function isEmpty(val) {
	// null or undefined
	if (val == null) return true;

	if (typeof val === 'boolean') return false;

	if (typeof val === 'number') return !val;

	if (val instanceof Error) return val.message === '';

	switch (Object.prototype.toString.call(val)) {
		// String or Array
		case '[object String]':
		case '[object Array]':
			return !val.length;

			// Map or Set or File
		case '[object File]':
		case '[object Map]':
		case '[object Set]': {
			return !val.size;
		}
		// Plain Object
		case '[object Object]': {
			return !Object.keys(val).length;
		}
	}

	return false;
}
